import Vue from 'vue'

function getItem(name, defValue, isCover = true) {
  let jsonStr = localStorage.getItem(name)
  let value
  if (jsonStr !== null) {
    value = JSON.parse(jsonStr)
  } else if (isCover) {
    if (defValue !== undefined) {
      value = defValue
      setItem(name, defValue)
    } else {
      // console.error('getItem defValue === undefined name = ' + name);
      return null
    }
  } else {
    return null
  }
  return value
}

function setItem(name, value) {
  if (name && (value !== undefined && value !== null)) {
    localStorage.setItem(name, JSON.stringify(value))
  } else {
    console.error('setItem error name = ' + name, 'value = ' + value);
  }
}

function removeItem(name) {
  localStorage.removeItem(name)
}

Vue.prototype.setItem = setItem
Vue.prototype.getItem = getItem
Vue.prototype.removeItem = removeItem